/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.battle;

import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;

/**
 *
 * @author Jakub Kutil
 */
public class UnitCountValidator implements IValidator<String> {

    @Override
    public void validate(IValidatable<String> iv) {
        final String field = iv.getValue();

        if (field.isEmpty()) {
            error(iv, "notNumber");

        }
        try {
            int number = Integer.parseInt(field);
            if (number < 1) {
                error(iv, "notPositive");
            }
        } catch (NumberFormatException e) {
            error(iv, "notNumber");
        }
    }

    private void error(IValidatable<String> validatable, String errorKey) {
        ValidationError error = new ValidationError();
        error.addMessageKey(getClass().getSimpleName() + "." + errorKey);
        validatable.error(error);
    }
}
